package com.example.bookkeeping_book.service;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.bookkeeping_book.R;
import com.example.bookkeeping_book.utility.MySqlite;
import com.example.bookkeeping_book.utility.Utility;

public class RegisterActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        init();
    }

    private void init() {
        ImageView back = findViewById(R.id.iv_back3);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        EditText account, password, db_password;
        account = findViewById(R.id.edt_account);

        password = findViewById(R.id.edt_password);

        db_password = findViewById(R.id.edt_again_password);

        Button btn = findViewById(R.id.btn_register);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s_account = account.getText().toString();
                String s_password = password.getText().toString();
                String sdb_password = db_password.getText().toString();

                if (s_account.equals("") || s_password.equals("") || sdb_password.equals("")) {
                    Toast.makeText(RegisterActivity.this, "请输入账号或密码", Toast.LENGTH_SHORT).show();
                } else if (!sdb_password.equals(s_password)) {
                    Toast.makeText(RegisterActivity.this, "两次密码不相同", Toast.LENGTH_SHORT).show();
                } else {
                    if (Utility.insertUser(new MySqlite(RegisterActivity.this), s_account, s_password)) {
                        Toast.makeText(RegisterActivity.this, s_account+ "注册成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(RegisterActivity.this, "已存在相同用户名", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}